require 'middleclass'
local vector2d = require("Vector2D")

Display = class('Display');

function Display:initialize(body)
    Display:initialize(body, nil)
end

function Display:initialize(body, sprite)
    self.body = body
    self.sprite = sprite
    body.cont = self
    --body removeself uses super
    body.cached_removeSelf = body.removeSelf
    function body:removeSelf()  
        print('removeDisplay')
        self.cont:clean()    
        self.cont = nil
        -- call original removeSelf() method
        self:cached_removeSelf()
    end
end

function Display:getX()
    return self.body.x
end

function Display:getY()
    return self.body.y
end

function Display:getVector()
    return vector2d:new(self.body.x, self.body.y)
end

function Display:setVector(vector)
    self.body.x = vector.x
    self.body.y = vector.y
end

function Display:getLinearVelocity()
    return self.body:getLinearVelocity()
end

function Display:setLinearVelocity(x,y)
    self.body:setLinearVelocity(x,y)
end

function Display:getLinearVelocityVec()
    return Vector2D:new(self.body:getLinearVelocity())
end

function Display:applyForce(vx, vy, x, y)
    self.body:applyForce(vx, vy, x, y)
end

function Display:applyLinearImpulse(vx, vy, x, y)
    self.body:applyLinearImpulse(vx, vy, x, y)
end

function Display:clean()
    --for extending
end

function Display:removeSelf()
    self.body:removeSelf()
end

function Display:addCollisionListener()
    --collision listener
    local function onBodyCollision(self, event)
        self.cont:collision(event)
    end
    self.body.collision = onBodyCollision
    self.body:addEventListener("collision", self.body)
end

function Display:addPreCollisionListener()
    --pre collision listener
    local function onBodyPreCollision(self, event)
        self.cont:preCollision(event)
    end
    self.body.preCollision = onBodyPreCollision
    self.body:addEventListener("preCollision", self.body)
end
